package cn.edu.njpi.web;

import cn.edu.njpi.model.*;
import cn.edu.njpi.service.*;
import cn.edu.njpi.utils.InformUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.web
 * @date 2022/4/20 7:02 PM
 */
@RequestMapping(value = "/class")
@Controller
public class ClassWeb {
    @Autowired
    private TClassService tClassService;
    @Autowired
    private TCollageService tCollageService;
    @Autowired
    private TSchoolService tSchoolService;
    @Autowired
    private TSchoolAdminService tSchoolAdminService;
    @Autowired
    private TCollageAdminService tCollageAdminService;
    @Value("${caoxingfen.url}")
    private String url;

    @RequestMapping(value = "/admin")
    public ModelAndView classAdmin(String token) {
        ModelAndView modelAndView = new ModelAndView("/class/admin");
        modelAndView.addObject("url", url);
        modelAndView.addObject("title", InformUtils.title);
        TUser tUser = (TUser) InformUtils.map.get(token);
        if (tUser.getUserPermssionId() == 1) {
            List<TClass> tClassList = tClassService.getAllClass();
            List<TSchool> allSchool = tSchoolService.getAllSchool();
            List<TCollage> allCollage = tCollageService.getAllCollage();
            for (TCollage tCollage : allCollage) {
                tCollage.setCollageName(tCollage.getSchoolName() + "-" + tCollage.getCollageName());
            }
            modelAndView.addObject("tSchoolList", allSchool);
            modelAndView.addObject("tCollageList", allCollage);
            modelAndView.addObject("tClassList", tClassList);
        } else if (tUser.getUserPermssionId() == 2) {
            TSchoolAdmin tSchoolAdmin = tSchoolAdminService.selectTSchoolAdminByUserId(tUser.getUserId());
            List<TClass> tClassList = tClassService.selectClassBySchoolId(tSchoolAdmin.getSchoolId());
            TSchool tSchool = tSchoolService.selectTSchoolBySchoolId(tSchoolAdmin.getSchoolId());
            List<TCollage> tCollageList = tCollageService.selectCollageBySchoolId(tSchool.getSchoolId());
            for (TCollage tCollage : tCollageList) {
                tCollage.setCollageName(tCollage.getSchoolName() + "-" + tCollage.getCollageName());
            }
            modelAndView.addObject("tCollageList", tCollageList);
            modelAndView.addObject("tClassList", tClassList);
        } else if (tUser.getUserPermssionId() == 3) {
            TCollageAdmin tCollageAdmin = tCollageAdminService.selectTCollageAdminByUserId(tUser.getUserId());
            List<TClass> tClassList = tClassService.selectClassByCollageId(tCollageAdmin.getCollageId());
            List<TCollage> tCollageList = new ArrayList<>();
            TCollage tCollage = tCollageService.selectCollageByCollageId(tCollageAdmin.getCollageId());
            TSchool tSchool = tSchoolService.selectTSchoolBySchoolId(tCollage.getSchoolId());
            tCollage.setCollageName(tSchool.getSchoolName() + "-" + tCollage.getCollageName());
            tCollageList.add(tCollage);
            modelAndView.addObject("tCollageList", tCollageList);
            modelAndView.addObject("tClassList", tClassList);
        }
        return modelAndView;
    }
}
